package com.innjia.sys.task;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.innjia.base.enums.ContractStatusEnum;
import com.innjia.base.enums.HouseStatusEnum;
import com.innjia.base.utils.DateUtils;
import com.innjia.sys.entity.ContractInfoEntity;
import com.innjia.sys.service.ContractInfoService;
import com.innjia.sys.service.HouseService;

@Component("contractTask")
public class ContractTask {

	private static Logger logger = Logger.getLogger(ContractTask.class);
	
	@Autowired
	private ContractInfoService contractInfoService;
	
	@Autowired
	private HouseService houseService;
	
	/**
	 * 查找所有未过期合同，判断合同有效截止日期是否小于当前时间，小于则更新合同："已作废"，更新房源状态为:"空置"
	 * void
	 */
	public void contractExpires() {
		Date exeTime = new Date();
		String startTime = DateUtils.format(exeTime, "yyyy-MM-dd HH:mm:ss");
		logger.info("===============合同自然到期，更新合同状态任务开始执行,开始执行时间：" + startTime + "===============");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("contractStatus", ContractStatusEnum.SIGNED.getCode());
		List<ContractInfoEntity> datas = contractInfoService.searchQueryList(params);
		if(datas != null && datas.size() > 0) {
			for(ContractInfoEntity entity : datas) {
				String time = DateUtils.format(entity.getContractValidityEndTime(), "yyyy-MM-dd HH:mm:ss");
				if(time.compareTo(startTime) < 0) {
					entity.setContractStatus(ContractStatusEnum.TERMINATING.getCode());
					entity.setContractTerminationDate(exeTime);
					entity.setUpdateTime(exeTime);
					if(contractInfoService.update(entity) > 0) {
						if(!houseService.updateRentStatusByHouseId(entity.getHouseId(), HouseStatusEnum.FREE.getDesc())) {
							logger.info("==========房态更新失败，合同id：contractInfoId：" + entity.getId() + ";房源id：houseId:" + entity.getHouseId());
						};
					} else {
						logger.info("===========合同装太更新失败，合同id：contractInfoId：" + entity.getId());
					}
					logger.info("===========合同id:" + entity.getId() + "，该合同已到期作废，房源可租========");
				}
			}
		}
		String endTime = DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
		logger.info("===============合同自然到期，更新合同状态任务执行结束,执行结束时间：" + endTime + ";耗时：" 
				+ (System.currentTimeMillis() - exeTime.getTime()) + "ms===============");
	}
	
	/**
	 * 合同创建完成，甲方在72小时未签约合同失效；甲方签约成功后乙方72小时内未签约合同失效
	 * 
	 * void
	 */
	public void contractSignExpired() {
		Date date = new Date();
		String startTime = DateUtils.format(date, "yyyy-MM-dd HH:mm:ss");
		logger.info("===============合同签约时效已过，更新合同状态任务开始执行,开始执行时间：" + startTime + "===============");
        List<ContractInfoEntity> datas = contractInfoService.queryByTenantOrLandlordPhone(null);
        if(datas != null && datas.size() > 0) {
        	logger.info("===>>>本次任务已过签约时效合同数量：" + datas.size());
        	datas.forEach(contract -> {
	      		contract.setContractStatus(ContractStatusEnum.TERMINATING.getCode());
	      		contract.setContractTerminationDate(date);
	      		contract.setUpdateTime(date);
	            contractInfoService.update(contract);
	            logger.info("===>>>contractInfoId :" + contract.getId() + "已过签约时效，合同状态更新成功");
	      	});
        } else {
        	logger.info("===>>>本次任务已过签约时效合同数量为0");
        }
		String endTime = DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
		logger.info("===============合同签约时效已过，更新合同状态任务执行结束,执行结束时间：" + endTime + "；耗时：" 
				+ (System.currentTimeMillis() - date.getTime()) + "ms===============");
	}
}
